package com.leanyuan.Infor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.Infor.api.Result;
import com.leanyuan.Infor.dto.HouseDto;
import com.leanyuan.publi.domain.Building;
import com.leanyuan.publi.domain.House;
import com.leanyuan.publi.service.BuildingService;
import com.leanyuan.publi.service.HouseService;
import com.leanyuan.publi.service.TParkinglotService;
import com.leanyuan.publi.service.TParkingspaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fangwu")
public class FangwuController {

    @Autowired
    BuildingService buildingService;

    @Autowired
    HouseService houseService;
    @Autowired
    TParkingspaceService tParkingspaceService;

    @RequestMapping("/del2")
    public Result<Object> del2(@RequestBody Map<String,Integer> param){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("house_id",param.get("id"));
        houseService.remove(wrapper);
        return Result.ok();
    }
    @RequestMapping("/Info")
    public Result<Object> find(@RequestBody Map<String,Object> param){
        Integer page=Integer.valueOf(String.valueOf(param.get("page")));
        Integer pagesize=Integer.valueOf(String.valueOf(param.get("pageSize")));
        Integer id = null;
        String username=null;
        String lname = null;
        if(param.get("id")!=null&&param.get("id")!=""){
            id=Integer.valueOf(String.valueOf(param.get("id")));
        }
        if(param.get("username")!=null&&param.get("username")!=""){
            username=String.valueOf(param.get("username"));
        }
        if(param.get("lname")!=null&&param.get("lname")!=""){
            lname=String.valueOf(param.get("lname"));
        }
         IPage<House> iPage=houseService.findAllp(new Page<House>(page,pagesize),id,lname,username);
        System.out.println(iPage.getRecords());
        return Result.ok(iPage);
    }

    @RequestMapping("/loudong")
    public Result<Object> loudong(){
        List<Building> building2=buildingService.list(null);
        return Result.ok(building2);
    }

    @RequestMapping("/upd")
    public Result<Object> upd(@RequestBody Map<String,Object> param){
        Integer houseId=Integer.valueOf(String.valueOf(param.get("houseId")));
        Float hrea=Float.valueOf(String.valueOf(param.get("hArea")));
        Integer hname=Integer.valueOf(String.valueOf(param.get("hname")));
        String lname=String.valueOf(param.get("lname"));
        House house=new House(houseId,hname,hrea,new Building(lname));
        System.out.println(house);
        int i=houseService.upd(house);
        if (i > 0) {
            return Result.ok();
        }
        return Result.error("修改信息有误");
    }

    @RequestMapping("/add")
    public Result<Object> add(@RequestBody HouseDto dto){
        House house=new House();
        house.setName(dto.getHname());
        house.setHArea(dto.getArea());
        house.setBuilding(new Building(dto.getLname()));
        int i=houseService.ins(house);
        if(i>0){
            return Result.ok();
        }
        return Result.error("添加信息有误");
    }

    @RequestMapping("/del")
    public Result<Object> del(@RequestBody Integer id){
        QueryWrapper<House> wrapper=new QueryWrapper<House>();
        wrapper.eq("house_id",id);
        Boolean flas=houseService.remove(wrapper);
        if(flas){
            return Result.ok();
        }
        return Result.error("删除信息有误");
    }

    @RequestMapping("/statistics")
    public Result<Object> statistics(){
        return Result.ok(houseService.findCount());
    }
    @RequestMapping("/spaceStat")
    public Result<Object> spaceStat(){
        return Result.ok(tParkingspaceService.findCount());
    }


}
